home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Magazine / C_Tutorial / Part-13 / PatchLib / source / StartUp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-12-10  |  6.8 KB  |  293 lines

  1. /*
  2. **    patch.library
  3. **
  4. **    Copyright © 1993-1997 by Stefan Fuchs
  5. **
  6. **      This file is based on:
  7. **
  8. **      $VER: StartUp.c 1.3 (13.9.96)
  9. **
  10. **      Library startup-code and function table definition
  11. **
  12. **      (C) Copyright 1996 Andreas R. Kleinert
  13. **      All Rights Reserved.
  14. **
  15. */
  16.  
  17. #ifndef _PATCH_INCLUDES_H
  18. #include "patch_includes.h"
  19. #endif
  20.  
  21.  
  22. struct PatchBase * LIBFUNC InitLib( REGA6 struct ExecBase  * GNUC_REGA6,
  23.                                     REGA0 BPTR               GNUC_REGA0,
  24.                                     REGD0 struct PatchBase * GNUC_REGD0);
  25.  
  26. struct PatchBase * ASMFUNC OpenLib( REGA6 struct PatchBase * GNUC_REGA6);
  27. BPTR  ASMFUNC CloseLib( REGA6 struct PatchBase * GNUC_REGA6);
  28. BPTR  LIBFUNC ExpungeLib( REGA6 struct PatchBase * GNUC_REGA6);
  29. ULONG ASMFUNC ExtFuncLib(void);
  30.  
  31. ULONG ASMFUNC MemHandlerCode( REGA0 struct Interrupt * GNUC_REGA0, REGA1 struct PatchBase * GNUC_REGA1);
  32.  
  33.  
  34. LONG ASMFUNC LibStart(void)
  35. {
  36.     return(-1);
  37. }
  38.  
  39. extern APTR FuncTab [];
  40. extern struct MyDataInit DataTab;
  41. extern char LibName;
  42.  
  43. struct InitTable                       /* do not change */
  44. {
  45.  ULONG              LibBaseSize;
  46.  APTR              *FunctionTable;
  47.  struct MyDataInit *DataTable;
  48.  APTR               InitLibTable;
  49. } InitTab =
  50. {
  51.  sizeof(struct PatchBase),
  52.  &FuncTab[0],
  53.  &DataTab,
  54.  InitLib
  55. };
  56.  
  57. APTR FuncTab [] =                      /* add your own functions here */
  58. {
  59.     OpenLib,
  60.     CloseLib,
  61.     ExpungeLib,
  62.     ExtFuncLib,
  63.  
  64.     InstallPatch,        /* V1    obsolete */
  65.     WaitRemovePatch,    /* V1    obsolete */
  66.     RemovePatch,        /* V1    obsolete */
  67.     FindPatch,        /* V1    obsolete */
  68.  
  69.     InstallPatchTagsA,    /* V2 */
  70.     RemovePatchTagsA,    /* V2 */
  71.  
  72.     FindPatchTagsA,        /* V3 */
  73.     SetPatchA,        /* V3 */
  74.     GetPatchA,        /* V3 */
  75.     PatchFreeVec,        /* V3 */
  76.  
  77.     RemovePatchProjectA,    /* V4 */
  78.     CreatePatchProjectA,    /* V4 */
  79.  
  80.     SetPatchProjectA,    /* V5 */
  81.     GetPatchProjectA,    /* V5    (reserved) */
  82.     AddPatchNotifyA,    /* V5 */
  83.     RemPatchNotifyA,    /* V5 */
  84.     PatchAlloc,        /* V5 */
  85.  
  86. /*    InformWedge,           V6? */
  87. /*    InternalInstallPatch,       V6? */
  88. /*    InternalRemovePatch,       V6? */
  89.  
  90.     RemovePatchHandler,    /* PRIVATE */
  91.  
  92.     (APTR) ((LONG)-1)
  93. };
  94.  
  95.  
  96. struct PatchBase    *PatchBase    = NULL;
  97. struct ExecBase        *SysBase    = NULL;
  98. struct DosLibrary    *DOSBase    = NULL;
  99. #ifdef __GNUC__
  100.     struct UtilityBase    *UtilityBase    = NULL;
  101.     struct Library *__UtilityBase = NULL;
  102. #else
  103.     struct Library        *UtilityBase    = NULL;
  104. #endif
  105.  
  106.  
  107.  
  108. struct Interrupt MemHandler;
  109.  
  110. struct PatchBase * LIBFUNC InitLib( REGA6 struct ExecBase  *sysb    GNUC_REGA6,
  111.                                     REGA0 BPTR             seglist  GNUC_REGA0,
  112.                                     REGD0 struct PatchBase *patchb  GNUC_REGD0)
  113. {
  114.     ULONG negsize, possize, fullsize;
  115.     UBYTE *negptr;
  116.  
  117.     PatchBase = patchb;
  118.     SysBase = sysb;
  119.     PatchBase->PB_SegList = seglist;
  120.  
  121.     if(SysBase->LibNode.lib_Version >= 37) /* patch.library V6 requires kick V37+ */
  122.     {
  123.         if( DOSBase = (APTR) OpenLibrary("dos.library", 0L)) 
  124.         {
  125.             if( UtilityBase = (APTR) OpenLibrary("utility.library", 0L))
  126.             {
  127.                 PatchBase->PB_SysBase = SysBase;
  128.                 PatchBase->PB_UtilityBase = (APTR)UtilityBase;
  129.                 PatchBase->PB_DosBase = (APTR)DOSBase;
  130.  
  131. #ifdef __GNUC__
  132.                 __UtilityBase = (APTR)UtilityBase;
  133. #endif
  134.  
  135.                 NewList(&(PatchBase->PB_MasterPatchHeader));
  136.                 NewList(&(PatchBase->PB_NotifyListHeader));
  137.                 NewList(&(PatchBase->PB_ProjectHeader));
  138.                 PatchBase->PB_Semaphore.ss_Link.ln_Type = NT_SIGNALSEM;
  139.                 InitSemaphore(&(PatchBase->PB_Semaphore));
  140.  
  141.                 Forbid();
  142.                 PatchBase->PB_Semaphore.ss_Link.ln_Name = &LibName;
  143.                 Enqueue(&(sysb->SemaphoreList), (struct Node *) &(PatchBase->PB_Semaphore));
  144.                 Permit();
  145.  
  146.                 PatchBase->PB_OffsetCodeStart = offsetof( struct PatchCode, PC_PatchCodeStart);
  147.  
  148.                 PatchBase->PB_MemoryPool = LibCreatePool( MEMF_CLEAR| MEMF_PUBLIC, 0x800, 0x200);
  149.  
  150.                 if(SysBase->LibNode.lib_Version >= 39)
  151.                 {
  152.                     MemHandler.is_Node.ln_Pri = 50;
  153.                     MemHandler.is_Node.ln_Name = &LibName;
  154.                     MemHandler.is_Data = PatchBase;
  155.                     MemHandler.is_Code = (void (*) ())MemHandlerCode;
  156.                     AddMemHandler(&MemHandler);
  157.                 }
  158.                 SetupAsmEnv(PatchBase);
  159. /* Set some default values */
  160.                 PatchBase->PB_StackExtensions = 5;
  161.                 PatchBase->PB_Flags |= PBF_NoStackAllocmem;
  162.  
  163.                 return(PatchBase);
  164.             }
  165.             CloseLibrary((APTR)DOSBase);
  166.         }
  167.     }
  168.  
  169.     negptr = (UBYTE *) PatchBase;
  170.  
  171.     negsize  = PatchBase->PB_LibNode.lib_NegSize;
  172.     possize  = PatchBase->PB_LibNode.lib_PosSize;
  173.     fullsize = negsize + possize;
  174.     negptr  -= negsize;
  175.  
  176.     FreeMem(negptr, fullsize);
  177.     return( NULL);
  178. }
  179.  
  180. struct PatchBase * ASMFUNC OpenLib( REGA6 struct PatchBase *patchb GNUC_REGA6)
  181. {
  182.     patchb->PB_LibNode.lib_OpenCnt++;
  183.     patchb->PB_LibNode.lib_Flags &= ~LIBF_DELEXP;
  184.     return(patchb);
  185. }
  186.  
  187. BPTR ASMFUNC CloseLib( REGA6 struct PatchBase *patchb GNUC_REGA6)
  188. {
  189.     patchb->PB_LibNode.lib_OpenCnt--;
  190.  
  191.     if(!patchb->PB_LibNode.lib_OpenCnt)
  192.     {
  193.         if(patchb->PB_LibNode.lib_Flags & LIBF_DELEXP)
  194.         {
  195.             return( ExpungeLib(patchb) );
  196.         }
  197.     }
  198.     return(NULL);
  199. }
  200.  
  201.  
  202.  
  203. BPTR LIBFUNC ExpungeLib( REGA6 struct PatchBase *PatchBase GNUC_REGA6)
  204. {
  205.     BPTR seglist;
  206.  
  207.     if(PatchBase->PB_LibNode.lib_OpenCnt == NULL)
  208.     {
  209.         ULONG negsize, possize, fullsize;
  210.         UBYTE *negptr = (UBYTE *) PatchBase;
  211.  
  212.         RemovePatchHandler();
  213.  
  214.         if(PatchBase->PB_MasterPatchHeader.lh_Head->ln_Succ)
  215.         {
  216.             PatchBase->PB_LibNode.lib_Flags |= LIBF_DELEXP;
  217.             return(NULL);
  218.         }
  219.  
  220.  
  221.         if( PatchBase->PB_PatchFunctionCounter)
  222.         {
  223.             PatchBase->PB_LibNode.lib_Flags |= LIBF_DELEXP;
  224.             return(NULL);
  225.         }
  226.  
  227.         if(SysBase->LibNode.lib_Version >= 39)
  228.         {
  229.             RemMemHandler(&MemHandler);
  230.         }
  231.  
  232.         RemSemaphore(&(PatchBase->PB_Semaphore));
  233.         if (PatchBase->PB_MemoryPool)
  234.         {
  235.             LibDeletePool(PatchBase->PB_MemoryPool);
  236.         }
  237.  
  238.         seglist = PatchBase->PB_SegList;
  239.  
  240.         Remove((struct Node *)PatchBase);
  241.  
  242.         if(UtilityBase) CloseLibrary((APTR)UtilityBase);
  243.         if(DOSBase) CloseLibrary((APTR)DOSBase);
  244.  
  245.         negsize  = PatchBase->PB_LibNode.lib_NegSize;
  246.         possize  = PatchBase->PB_LibNode.lib_PosSize;
  247.         fullsize = negsize + possize;
  248.         negptr  -= negsize;
  249.  
  250.         FreeMem(negptr, fullsize);
  251.  
  252.         return(seglist);
  253.     }
  254.  
  255.     PatchBase->PB_LibNode.lib_Flags |= LIBF_DELEXP;
  256.     return(NULL);
  257. }
  258.  
  259. ULONG ASMFUNC ExtFuncLib(void)
  260. {
  261.  return(NULL);
  262. }
  263.  
  264. ULONG LIBFUNC MemHandlerCode( REGA0 struct Interrupt *memhandler GNUC_REGA0, REGA1 struct PatchBase *PatchBase GNUC_REGA1)
  265. {
  266.     RemovePatchHandler();
  267.     return(MEM_ALL_DONE);
  268. }
  269.  
  270.  
  271. #if defined(__SASC)
  272.  
  273. /* for SAS/C's sc.lib only */
  274.  
  275. ULONG XCEXIT       = NULL;    /* these symbols may be referenced by    */
  276. ULONG _XCEXIT      = NULL;    /* some functions of sc.lib, but should  */
  277. ULONG ONBREAK      = NULL;    /* never be used inside a shared library */
  278. ULONG _ONBREAK     = NULL;
  279. ULONG base         = NULL;
  280. ULONG _base        = NULL;
  281. ULONG ProgramName  = NULL;
  282. ULONG _ProgramName = NULL;
  283. ULONG StackPtr     = NULL;
  284. ULONG _StackPtr    = NULL;
  285. ULONG oserr        = NULL;
  286. ULONG _oserr       = NULL;
  287. ULONG OSERR        = NULL;
  288. ULONG _OSERR       = NULL;
  289.  
  290. void __regargs __chkabort(void) { }  /* a shared library cannot be    */
  291. void __regargs _CXBRK(void)     { }  /* CTRL-C aborted when doing I/O */
  292.  
  293. #endif